/**
 * unPlarer-Youtube 0.8.7
 *
 * @author Alvaro Lopez, Juan Baquero
 * 
 * http://code.google.com/p/unplayer/
 **/

package unplayer.youtubeplayer.util;

import java.io.File;
import java.io.UnsupportedEncodingException;

import processing.core.PApplet;

public class UrlStream {
  
  public UrlStream(){
	  isActive=false;
  }

  public static boolean isValidUrl(String uri)
  {
	  boolean rst = true;
	  if(uri.length()<42) rst = false;
	  if(uri.indexOf("http://www.youtube.com")!=0) rst = false;
	  return rst;
  }
  
  public static String decodeString(String uri)
  {
	  uri = uri.replace("&lt;", "<");
	  uri = uri.replace("&gt;", ">");
	  uri = uri.replace("&quot;", "\"");
	  uri = uri.replace("&#039;", "\'");
	  uri = uri.replace("&amp;", "&");
	  uri = uri.replace("&#x201E;", "\"");
	  uri = uri.replace("&nbsp;", "\"");

	  int fo =0;
	  int so =0;
	  while(fo!=-1 && so!=-1)
	  {
		  fo = uri.indexOf("&",so);
		  if(fo!=-1) so = uri.indexOf(";",fo);

		  if(fo!=-1 && so!=-1)
		  {
			  if(uri.substring(fo+1, so+1).indexOf("&")!=-1)
			  {
				  so=fo+1;
				  continue;
			  }
			  
			  uri = uri.replace(uri.substring(fo, so+1),"");
			  so=0;
			  fo=0;
		  }
	  }
	  
	  return uri;
  }
  
  public void loadUrl(String uri, int size)
  {
	  if(!isValidUrl(uri))
	  {
		  error = NO_YOUTUBE;
		  chargeg=true;
		  connection=null;
		  isActive=false;
		  return;
	  }
		  
	  connection = new HttpConnection(this, uri, size);
	  isActive=true;
	  chargeg=false;
	  
	  loaded=false;
	  connection.start();
  }
  
  public String getUrlStream()
  {
	  return urlStream;
  }
  
  public String getTitle()
  {
	  return title;
  }
  
  public void loadStream(PApplet pa, String str)
  {

	  //file= pa.sketchPath+File.separator+"data"+File.separator+"cache";
	  
	  String fileStream;

	  if(PApplet.platform == PApplet.WINDOWS && pa.online)
		{
			file = System.getProperty("java.io.tmpdir");
			file= file+"unpycache.tmp";
			fileStream=file;
		}else
		{
			file="cache";
			fileStream= "data"+File.separator+file;
		}
	  
	  
//	  file="cache";
//	  String pipeline = "souphttpsrc location="+str+" ! filesink location="+file;
//	  pipe = new GSPipeline(pa, pipeline);  
	  
	  //LOAD STREAM
	  videoCache = new StreamVideo(this);
	  videoCache.loadVideo(pa, str,fileStream);
	  videoCache.start();
	  
	  //VERIFY STREAM
	  hdCache = new LogicalCache(this,pa,file);
	  isActive=true;
	  loaded=false;
	  hdCache.start();
  }
  
  public String getFile()
  {
	 return file; 
  }
  
  //USADO POR HILOS
  
  public void setUrlStream(String url,String tit,int msg)
  {
	  error = msg;
	  urlStream=url;
	  try {
		title=decodeString(new String (tit.getBytes(), "UTF-8"));
	} catch (UnsupportedEncodingException e) {
		//e.printStackTrace();
	}
	  chargeg=true;
	  connection=null;
	  isActive=false;
  }

  
  public void loadResults(int err)
  {
	  isActive=false;
	  loaded=true;
	  error = err;
  }
  
  public void closeVideo(PApplet parent)
  {	  		  
	  
	  	  if(hdCache!=null) hdCache.broke = true;;
	  	  
		  if(videoCache==null)
			  return;
			  
		  videoCache.close();
		  
		  File filep = parent.dataFile(file);
		  int cont =0;
		  while(!filep.delete())
		  {
			  cont++;
			  if(cont==4000)
				  break;
		  }
  }
  
  public boolean isActive;
  public boolean chargeg;
  public boolean loaded;
  public int error;
  
  private String title;
  private String urlStream;
  private String file;
  private HttpConnection connection;
  private LogicalCache hdCache;
  private StreamVideo videoCache;
  
  //private GSPipeline pipe;

  //ERRORES
  
  public static final int OK = 0;  // OK
  public static final int LAZY_CONNECTION = 1; 
  public static final int NO_CONNECTION = 2; 
  public static final int NO_SIZE= 3; 
  public static final int URL_ERROR= 4; 
  public static final int NO_YOUTUBE= 5; 
  public static final int NO_VIDEO= 6; 
  
}
